import {Component, OnInit} from '@angular/core';
import {TipService} from "../../../services/tip.service";
import {ExtendHttpService} from "../../../services/extend-http.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-withdraw-cash-apply',
  templateUrl: './withdraw-cash-apply.component.html',
  styleUrls: ['./withdraw-cash-apply.component.css']
})
export class WithdrawCashApplyComponent implements OnInit {

  tipType = 'error';
  account = '';
  price: string;
  isLoading = false;
  submitFlag = true;

  constructor(private tipService: TipService,
              private extendHttp: ExtendHttpService,
              private router: Router) {
  }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    this.getInfo();
  }

  getInfo() {
    this.extendHttp.get('/agentApp/withdrawals/getZhifb', {}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.account = res.zfbNumber;
      } else if (res['code'] == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 1500);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  submitData() {
    if (/^[1-9]\d*$/.test(this.price)) {
      if (Number(this.price) >= 100) {
        this.isLoading = !this.isLoading;
        this.submitFlag = !this.submitFlag;
        this.extendHttp.post('/agentApp/withdrawals/toapply', {money: this.price}).subscribe(response => {
          let res = response.json();
          if (res['code'] === '0') {
            this.isLoading = !this.isLoading;
            this.tipType = 'success';
            this.tipService.change(true, res['message']);
            setTimeout(() => {
              this.router.navigateByUrl('/main/withdraw-cash/withdraw-cash-index');
            }, 1500);
          } else if (res['code'] === '-2001') {
            this.tipType = 'error';
            this.tipService.change(true, res['message']);
            setTimeout(() => {
              this.router.navigateByUrl('/login');
            }, 1500);
          } else {
            this.isLoading = !this.isLoading;
            this.submitFlag = !this.submitFlag;
            this.tipType = 'error';
            this.tipService.change(true, res['message']);
          }
        }, error => {
          this.isLoading = !this.isLoading;
          this.submitFlag = !this.submitFlag;
          this.tipType = 'error';
          this.tipService.change(true, '系统繁忙，操作失败！');
        });
      } else {
        this.tipService.change(true, '请输入大于100的正整数');
      }
    } else {
      this.tipService.change(true, '请输入大于100的正整数');
    }


  }

}
